package net.pinnoocle.playservice.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.pinnoocle.playcommon.utils.HelpUtils;
import net.pinnoocle.playdao.dao.ShopStatisticsDao;
import net.pinnoocle.playdao.entity.ShopStatistics;
import net.pinnoocle.playdao.entity.ShopUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 流量分析(ShopStatistics)表服务实现类
 *
 * @author ye
 * @since 2021-11-30 11:19:33
 */
@Service("shopStatisticsService")
public class ShopStatisticsServiceImpl extends ServiceImpl<ShopStatisticsDao, ShopStatistics> {

    @Autowired
    ShopStatisticsServiceImpl shopStatisticsService;
    @Autowired
    ShopUserServiceImpl shopUserService;

    /**
     * 累计门店访客数、流量量
     * @param shopId
     * @param yesterday
     * @param isNew
     */
    public void grandShopStatisticsVisit(String shopId,Boolean yesterday,Boolean isNew){
        String today = DateUtil.today();
        ShopStatistics shopStatisticsToday = shopStatisticsService.getOne(new QueryWrapper<ShopStatistics>().lambda()
                .eq(ShopStatistics::getShopId, shopId)
                .eq(ShopStatistics::getDateTime, today));
        if(Objects.isNull(shopStatisticsToday)){
            ShopStatistics shopStatistics = new ShopStatistics();
            shopStatistics.setShopId(shopId);
            shopStatistics.setVisitorNum(1);
            shopStatistics.setPageView(1);
            shopStatistics.setAddUserNum(1);
            shopStatistics.setDateTime(today);
            shopStatisticsService.save(shopStatistics);
        }else{
            shopStatisticsToday.setPageView(shopStatisticsToday.getPageView()+1);
            if(yesterday){
                shopStatisticsToday.setVisitorNum(shopStatisticsToday.getVisitorNum()+1);
            }
            if(isNew){
                shopStatisticsToday.setAddUserNum(shopStatisticsToday.getAddUserNum()+1);
            }
            shopStatisticsService.updateById(shopStatisticsToday);
        }
    }

    /**
     * 增加组局访问量
     * @param shopId
     * @param shopUser
     */
    public void grandShopStatisticsGroup(String shopId, ShopUser shopUser){
        String today = DateUtil.today();
        ShopStatistics shopStatisticsToday = shopStatisticsService.getOne(new QueryWrapper<ShopStatistics>().lambda()
                .eq(ShopStatistics::getShopId, shopId)
                .eq(ShopStatistics::getDateTime, today));
        boolean yesterday = HelpUtils.isYesterday(shopUser.getLastVisitGroupTime());
        if(Objects.isNull(shopStatisticsToday)){
            ShopStatistics shopStatistics = new ShopStatistics();
            shopStatistics.setShopId(shopId);
            shopStatistics.setGroupVisitorNum(1);
            shopStatistics.setDateTime(today);
            shopStatisticsService.save(shopStatistics);
        }else{
            if(yesterday){
                shopStatisticsToday.setGroupVisitorNum(shopStatisticsToday.getGroupVisitorNum()+1);
                shopStatisticsService.updateById(shopStatisticsToday);
            }
        }
        //更新访问时间
        shopUser.setLastVisitGroupTime(new Date());
        shopUserService.updateById(shopUser);
    }

    /**
     * 剧本访问量
     * @param shopId
     */
    public void grandShopStatisticsScript(String shopId){
        String today = DateUtil.today();
        ShopStatistics shopStatisticsToday = shopStatisticsService.getOne(new QueryWrapper<ShopStatistics>()
                .lambda()
                .eq(ShopStatistics::getShopId, shopId)
                .eq(ShopStatistics::getDateTime, today));
        if(Objects.isNull(shopStatisticsToday)){
            ShopStatistics shopStatistics = new ShopStatistics();
            shopStatistics.setShopId(shopId);
            shopStatistics.setScriptVisitorNum(1);
            shopStatistics.setDateTime(today);
            shopStatisticsService.save(shopStatistics);
        }else{
            shopStatisticsToday.setScriptVisitorNum(shopStatisticsToday.getScriptVisitorNum()+1);
            shopStatisticsService.updateById(shopStatisticsToday);
        }
    }

    /**
     * 付费用户数、订单数累计
     * @param shopId
     * @param playTimes
     */
    public void grandShopStatisticsOrder(String shopId, int playTimes){
        String today = DateUtil.today();
        ShopStatistics shopStatisticsToday = shopStatisticsService.getOne(new QueryWrapper<ShopStatistics>().lambda()
                .eq(ShopStatistics::getShopId, shopId)
                .eq(ShopStatistics::getDateTime, today));
        if(Objects.isNull(shopStatisticsToday)){
            ShopStatistics shopStatistics = new ShopStatistics();
            shopStatistics.setShopId(shopId);
            shopStatistics.setPayUserNum(1);
            shopStatistics.setPayOrderNum(1);
            shopStatistics.setDateTime(today);
            shopStatisticsService.save(shopStatistics);
        }else{
            //记录新付费用户
            if(playTimes == 0){
                shopStatisticsToday.setPayUserNum(shopStatisticsToday.getPayUserNum()+1);
            }else if(playTimes == 1){ //记录老付费用户
                shopStatisticsToday.setPayUserNum(shopStatisticsToday.getOldPayUserNum()+1);
            }
            shopStatisticsToday.setPayOrderNum(shopStatisticsToday.getPayOrderNum()+1);
            shopStatisticsService.updateById(shopStatisticsToday);
        }
    }
}